package com.zzyl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.zzyl.dto.BedDto;
import com.zzyl.entity.Bed;
import com.zzyl.enums.BasicEnum;
import com.zzyl.exception.BaseException;
import com.zzyl.mapper.BedMapper;
import com.zzyl.service.BedService;
import com.zzyl.vo.BedVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class BedServiceImpl implements BedService {

    @Autowired
    private BedMapper bedMapper;

    @Override
    public List<BedVo> getBedsByRoomId(Long roomId) {
        return bedMapper.getBedsByRoomId(roomId);
    }

    // 床位新增
    @Override
    public int addBed(BedDto bedDto) {
        Bed bed = BeanUtil.copyProperties(bedDto, Bed.class);
        bed.setBedStatus(0);
        int num=bedMapper.countByBedNumber(bedDto.getBedNumber());
        try {
            if(num>0){
                throw new BaseException(BasicEnum.BED_INSERT_FAIL_DUPLICATION_BED_NUM);
            }
        } catch (BaseException e) {
            throw new RuntimeException(e);
        }




        return bedMapper.insertBed(bed);
    }

    // 根据id删除床位
    @Override
    public int deleteBedById(Long id) {
        // 判断床位是否有人使用，有人，则不能删除
        return bedMapper.deleteBedById(id);
    }

    // 修改床位信息
    @Override
    public int updateBed(Bed bed) {
        // 添加更新时间
        bed.setUpdateTime(LocalDateTime.now());
        return bedMapper.updateBed(bed);
    }

    // 根据id查询床位信息
    @Override
    public BedVo readBedById(Long id) {
        return bedMapper.readBedById(id);
    }




}

